package cn.blunce.view_utils.ChartHelp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

/**
 * AChartEngine简单使用
 * <p>
 * Created by Blunce on 2015/11/5 0005.
 */
public class ChartHelper extends BaseChart {

    public static Intent excuteActivity(Context context) {
        String[] titles = new String[]{"Crete", "Corfu", "Thassos", "Skiathos"};//四条折线的名字

        List<double[]> x = new ArrayList<double[]>();//每条折线的x值
        for (int i = 0; i < titles.length; i++) {
            x.add(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
        }

        List<double[]> values = new ArrayList<double[]>();//每条折线的y值
        values.add(new double[]{12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
                13.9});
        values.add(new double[]{10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11});
        values.add(new double[]{5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6});
        values.add(new double[]{9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10});

        int[] colors = new int[]{Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW};//每条折线线的颜色
        PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE, PointStyle.DIAMOND,
                PointStyle.TRIANGLE, PointStyle.SQUARE};//每条折线转折点的形状
        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);

        //属性设置
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }
        renderer.setChartTitle("Average temperature");
        renderer.setXTitle("Month");
        renderer.setYTitle("Temperature");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(12.5);
        renderer.setYAxisMin(-10);
        renderer.setYAxisMax(40);
        renderer.setAxesColor(Color.LTGRAY);
        renderer.setLabelsColor(Color.LTGRAY);

        renderer.setXLabels(12);
        renderer.setYLabels(10);
        renderer.setShowGrid(true);
        renderer.setXLabelsAlign(Paint.Align.RIGHT);
        renderer.setYLabelsAlign(Paint.Align.RIGHT);
        renderer.setZoomButtonsVisible(true);
        renderer.setPanLimits(new double[]{-10, 20, -10, 40});
        renderer.setZoomLimits(new double[]{-10, 20, -10, 40});
        renderer.setBackgroundColor(Color.BLACK);
        renderer.setApplyBackgroundColor(true);

        XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
        XYSeries series = dataset.getSeriesAt(0);
        series.addAnnotation("Vacation", 6, 30);

        Intent intent = ChartFactory.getLineChartIntent(context, dataset, renderer,
                "Average temperature");
        return intent;
    }

    public static View excuteView(Context context) {
        String[] titles = new String[]{"Crete", "Corfu", "Thassos", "Skiathos"};//四条折线的名字

        List<double[]> x = new ArrayList<double[]>();//每条折线的x值
        for (int i = 0; i < titles.length; i++) {
            x.add(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
        }

        List<double[]> values = new ArrayList<double[]>();//每条折线的y值
        values.add(new double[]{12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
                13.9});
        values.add(new double[]{10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11});
        values.add(new double[]{5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6});
        values.add(new double[]{9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10});

        int[] colors = new int[]{Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW};//每条折线线的颜色
        PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE, PointStyle.DIAMOND,
                PointStyle.TRIANGLE, PointStyle.SQUARE};//每条折线转折点的形状
        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);

        //属性设置
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }

        renderer.setChartTitle("Average temperature");
        renderer.setXTitle("Month");
        renderer.setYTitle("Temperature");
        renderer.setXAxisMin(0.5);
        renderer.setXAxisMax(12.5);
        renderer.setYAxisMin(-10);
        renderer.setYAxisMax(40);
        renderer.setAxesColor(Color.LTGRAY);
        renderer.setLabelsColor(Color.LTGRAY);

        renderer.setXLabels(12);
        renderer.setYLabels(10);
        renderer.setShowGrid(true);
        renderer.setXLabelsAlign(Paint.Align.RIGHT);
        renderer.setYLabelsAlign(Paint.Align.RIGHT);
        renderer.setZoomButtonsVisible(true);
        renderer.setPanLimits(new double[]{-10, 20, -10, 40});
        renderer.setZoomLimits(new double[]{-10, 20, -10, 40});
        renderer.setBackgroundColor(Color.BLACK);
        renderer.setApplyBackgroundColor(true);

        XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
        XYSeries series = dataset.getSeriesAt(0);
        series.addAnnotation("Vacation", 6, 30);

        return ChartFactory.getLineChartView(context, dataset, renderer);
    }
}
